package de.zainodis.commons.component;

public abstract class Component implements IComponent {
   protected IEntity owner;

   public Component() {
	 this(null);
   }

   public Component(IEntity owner) {
	 this.owner = owner;
   }

   @Override
   public IEntity getOwner() {
	 return owner;
   }

   @Override
   public void onUpdate() {
	 // Default implementation does nothing
   }

   @Override
   public void onRemove() {
	 // Does nothing by default
   }

   /**
    * Routinely implements the equals method by a class compare. More complex
    * components should override this.
    */
   @Override
   public boolean equals(Object o) {
	 if (o != null) {
	    return getClass().equals(o.getClass());
	 }
	 return false;
   }

   /**
    * This should only be used (and not misused!) in cases where the owning
    * entity is not available upon component creation.
    * 
    * @param newOwner
    *           the owning entity.
    * @throws IllegalAccessError
    *            thrown (to prevent misuse), if the component already has an
    *            owner.
    */
   public void setOwner(IEntity newOwner) {
	 if (this.owner != null) {
	    throw new IllegalAccessError(
			"Owner of a component may only set if there was previously no owner set.");
	 }
	 this.owner = newOwner;
   }
}
